<template>
  <div class="container">
    <div class="breadcrumb-container">
      <a-breadcrumb separator=">">
        <a-breadcrumb-item>
          <NuxtLink to="/" class="more"> 首页</NuxtLink>
        </a-breadcrumb-item>
        <a-breadcrumb-item>
          <NuxtLink
            :to="`/classify?classifyId=${movieInfo?.category[0].id}`"
            class="more"
          >
            {{ movieInfo?.category[0].name }}
          </NuxtLink>
        </a-breadcrumb-item>
        <a-breadcrumb-item>
          <NuxtLink :to="`/movie/${movieInfo?.id}`" class="more">
            {{ movieInfo?.name }}
          </NuxtLink>
        </a-breadcrumb-item>
        <a-breadcrumb-item
          ><NuxtLink class="more" style="color: #000">
            {{ episodeInfo?.name }}
          </NuxtLink>
        </a-breadcrumb-item>
      </a-breadcrumb>
    </div>
    <a-row class="detail-box">
      <a-col
        :span="18"
        :xs="24"
        :sm="24"
        :md="14"
        :lg="16"
        :xl="16"
        :xxl="18"
        class="video-box"
      >
        <video
          id="video_pc_id"
          preload="false"
          :poster="episodeInfo?.cover"
          :src="episodeInfo?.mp4"
          tabindex="-1"
          controls
          playsinline
          controlslist="nodownload noremoteplayback noplaybackrate foobar"
        ></video>
      </a-col>
      <a-col
        :span="6"
        :xs="0"
        :sm="0"
        :md="10"
        :lg="8"
        :xl="8"
        :xxl="6"
        style="padding-left: 0.08rem"
      >
        <a-flex vertical class="plot-box">
          <div class="title">
            集
            <span class="current"
              >({{
                episodeState.data.findIndex(
                  (item) => item.id === episodeState.activeId
                ) + 1
              }}
              / {{ movieInfo?.episodeCount || 0 }})</span
            >
          </div>
          <a-flex class="tab-header" align="center">
            <div
              v-for="page in totalPages"
              :key="page"
              class="tab-title"
              :class="{ tabTitleActive: episodeState.currentPage === page }"
              @click="changePage(page)"
            >
              {{ (page - 1) * episodeState.pageSize + 1 }}-{{
                Math.min(page * episodeState.pageSize, episodeState.data.length)
              }}
            </div>
          </a-flex>
          <div class="tab-content">
            <NuxtLink
              :to="`/play/${movieInfo?.id}?episodeId=${item.id}`"
              class="link-text"
              v-for="(item, index) in currentPageData"
              :key="index"
              :class="{ linkTextActive: item.id === episodeState.activeId }"
              @click="episodeState.activeId = item.id"
            >
              {{
                index +
                1 +
                (episodeState.currentPage - 1) * episodeState.pageSize
              }}
              <AlignRightOutlined
                v-if="item.id === episodeState.activeId"
                class="align"
              />
              <LockFilled class="lock" v-if="item.free === 2" />
            </NuxtLink>
          </div>
        </a-flex>
      </a-col>
    </a-row>
    <div class="video-info">
      <h1>
        {{ movieInfo?.name }}
        <span>{{ episodeInfo?.name }}</span>
      </h1>
      <div class="video-star">
        <StarFilled />
        <span>{{ episodeInfo?.collectCount }}</span>
      </div>
      <div class="video-desc">
        {{ movieInfo?.name }} {{ episodeInfo?.name }}.<br />
        {{ movieInfo?.introduction }}
      </div>
      <div class="tag-box">
        <NuxtLink
          :to="`/classify?classifyId=${movieInfo?.category[0].id}`"
          class="tag-item"
        >
          {{ movieInfo?.category[0].name }}
        </NuxtLink>
      </div>
      <a-flex align="center" class="share-box">
        <div class="share-label">分享：</div>
        <img :src="facebook" alt="twitter" />
        <img :src="twitter" alt="twitter" />
        <img :src="instagram" alt="twitter" />
        <a-tooltip placement="top">
          <template #title>
            <span>点击复制链接</span>
          </template>
          <img :src="copyIcon" alt="copyIcon" class="copyIcon" />
        </a-tooltip>
      </a-flex>
    </div>
    <a-row>
      <a-col :span="24" :xs="0" :sm="0" :md="24" :lg="24">
        <a-flex class="title-wrap" align="center">
          <h2>剧集列表</h2>
          <div class="counts">({{ movieInfo?.episodeCount || 0 }} 集)</div>
        </a-flex>
        <a-row class="list-info" :gutter="10">
          <a-col
            :span="4"
            :md="8"
            :lg="6"
            :xl="6"
            :xxl="4"
            v-for="(item, index) in isSeeMore
              ? [...currentPageData].splice(0, 11)
              : currentPageData"
            :key="item.id"
          >
            <NuxtLink :to="`/play/${movieInfo?.id}?episodeId=${item.id}`">
              <a-flex class="list-item">
                <div class="img-box">
                  <img :alt="item.name" :src="item.cover" />
                </div>
                <div class="intro">
                  <div class="title">{{ movieInfo?.name }}</div>
                  <div class="page-num">{{ item?.name }}</div>
                </div>
              </a-flex>
            </NuxtLink>
          </a-col>
          <a-col
            :span="4"
            :md="8"
            :lg="6"
            :xl="6"
            :xxl="4"
            v-if="isSeeMore"
            @click="isSeeMore = false"
          >
            <a-flex justify="center" align="center" class="see-more">
              <span>查看更多</span>
              <DownCircleFilled />
            </a-flex>
          </a-col>
        </a-row>
      </a-col>
    </a-row>
  </div>
  <a-row>
    <a-col :xs="24" :sm="24" :md="0">
      <div class="h5-intro-box">
        <div class="intro-title">简介</div>
        <p class="intro">
          {{ movieInfo?.introduction }}
        </p>
      </div>
      <div class="episode-nav">
        <div class="catalog-box">
          <p>
            剧集列表<span>({{ movieInfo?.episodeCount }} 集)</span>
          </p>
          <RightOutlined />
        </div>
        <a-flex align="center" class="share-box">
          <div class="share-label">分享：</div>
          <img :src="facebook" alt="twitter" />
          <img :src="twitter" alt="twitter" />
          <img :src="instagram" alt="twitter" />
          <a-tooltip placement="top">
            <template #title>
              <span>点击复制链接</span>
            </template>
            <img :src="copyIcon" alt="copyIcon" class="copyIcon" />
          </a-tooltip>
        </a-flex>
      </div>
    </a-col>
  </a-row>
  <div class="container">
    <a-flex class="title-wrap" justify="space-between">
      <h2>猜你喜欢</h2>
    </a-flex>
    <a-row class="episode-list" :gutter="10">
      <a-col
        :span="4"
        :xs="8"
        :md="8"
        :lg="4"
        class="book-item"
        v-for="(item, index) in recommendedMovies"
        :key="index"
      >
        <NuxtLink :to="`/movie/${item.id}`" class="book-image">
          <img :alt="item.name" loading="lazy" :src="item.cover" />
          <div class="chapter-count">{{ item.episodeCount ?? 0 }} 集</div>
        </NuxtLink>
        <div class="book-name">{{ item.name }}</div>
        <div class="book-name-box">
          <div class="book-name-hover">{{ item.name }}</div>
          <div class="tag-box">
            <NuxtLink
              class="tag-item"
              :to="`/movie/${item.id}`"
              v-for="(category, index) in item?.category"
              :key="index"
            >
              {{ category }}
            </NuxtLink>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script setup lang="ts">
import {
  AlignRightOutlined,
  LockFilled,
  StarFilled,
  RightOutlined,
  DownCircleFilled,
} from "@ant-design/icons-vue"
import {
  twitter,
  instagram,
  facebook,
  copyIcon,
} from "@/assets/images/base64.js"
import { request as getMovieInfoRequest } from "@/api/server/mods/movie/getMovieInfo"
import { request as getRecommendationsRequest } from "@/api/server/mods/movie/getRecommendations"
import { request as getMoviesEpisodeRequest } from "@/api/server/mods/moviesEpisode/getMoviesEpisode"
interface MovieState extends defs.server.MovieEntity {
  episodeCount?: number
}
const $route = useRoute()
const isSeeMore = ref(true)
onMounted(() => {
  getMoviesEpisode()
  getMovieInfo()
  getRecommendedMovies()
})
const movieInfo = ref<MovieState>()
/** 获取电影详情 */
async function getMovieInfo() {
  try {
    const { data, success } = await getMovieInfoRequest(
      $route.params.id as string
    )
    if (success) {
      movieInfo.value = data
    }
  } catch (error: any) {
    errorTips(error.message)
  }
}
const episodeState = reactive({
  data: [] as defs.server.MoviesEpisodeEntity[],
  currentPage: 1,
  pageSize: 50,
  activeId: null as number | null,
})
const totalPages = computed(() =>
  Math.ceil(episodeState.data.length / episodeState.pageSize)
)
const currentPageData = computed(() => {
  const start = (episodeState.currentPage - 1) * episodeState.pageSize
  const end = start + episodeState.pageSize
  return episodeState.data.slice(start, end)
})
const episodeInfo = computed(() => {
  const episode = episodeState.data.find(
    (item) => item.id === episodeState.activeId
  )
  return episode
})
function changePage(page: number) {
  episodeState.currentPage = page
}
watch(
  () => $route.query.episodeId,
  (newVal) => {
    getMoviesEpisode()
  }
)
/** 获取电影剧集 */
async function getMoviesEpisode() {
  try {
    const { data, success } = await getMoviesEpisodeRequest(
      $route.params.id as string
    )
    if (success) {
      episodeState.data = data
      let index = 0
      if ($route.query.episodeId) {
        index = data.findIndex(
          (item: { id: number }) => item.id === Number($route.query.episodeId)
        )
      }
      episodeState.activeId = data[index].id
    }
  } catch (error: any) {
    errorTips(error.message)
  }
}
const recommendedMovies = ref<MovieState[]>([])
/** 获取推荐电影 */
async function getRecommendedMovies() {
  try {
    const { data, success } = await getRecommendationsRequest(
      $route.params.id as string
    )
    if (success) {
      recommendedMovies.value = data
    }
  } catch (error: any) {
    errorTips(error.message)
  }
}
</script>
<style lang="scss" scoped>
.breadcrumb-container {
  height: 0.8rem;
  padding: 0 0.12rem;
  background-color: transparent;
  display: flex;
  align-items: center;
  a {
    font-size: 0.18rem;
    line-height: 0.28rem;
  }
}
.detail-box {
  height: 7.08rem;
  .video-box {
    border-radius: 0.08rem 0.08rem 0 0;
    overflow: hidden;
    position: relative;
    height: 100%;

    video {
      width: 100%;
      height: 100%;
      object-fit: contain;
      background-color: #1a1a1a;
    }
  }
  .plot-box {
    background-color: rgba(0, 0, 0, 0.06);
    height: 100%;
    .title {
      overflow: hidden;
      display: flex;
      align-items: center;
      flex-direction: row;
      background-color: rgba(0, 0, 0, 0.8);
      height: 0.48rem;
      padding: 0 0.16rem;
      font-size: 0.2rem;
      font-weight: 500;
      line-height: 0.24rem;
      color: #fff;
      .current {
        line-height: 0.2rem;
        color: hsla(0, 0%, 100%, 0.4);
        font-size: 0.14rem;
        margin-left: 0.08rem;
      }
    }
    .tab-header {
      flex-direction: row;
      overflow-y: auto;
      padding: 0.02rem 0.08rem;
      height: 0.52rem;
      .tab-title {
        padding-right: 0.1rem;
        display: flex;
        align-items: center;
        height: 100%;
        font-size: 0.14rem;
        color: rgba(0, 0, 0, 0.6);
        cursor: pointer;
      }
      .tabTitleActive {
        color: #ff375f;
      }
    }
    .tab-content {
      font-weight: 700;
      font-size: 0.16rem;
      color: rgba(0, 0, 0, 0.5);
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      grid-template-rows: repeat(10, 1fr);
      grid-gap: 0.08rem;
      gap: 0.08rem;
      padding: 0 0.08rem;
      .link-text {
        position: relative;
        background-color: #fff;
        display: inline-block;
        width: 0.52rem;
        height: 0.52rem;
        line-height: 0.52rem;
        text-align: center;
        border-radius: 0.04rem;
        transition: all 0.3s;
        color: rgba(0, 0, 0, 0.5);
        .align {
          position: absolute;
          bottom: 0.04rem;
          left: 0.04rem;
          transform: rotateZ(90deg);
        }
        .lock {
          position: absolute;
          top: 0.04rem;
          right: 0.04rem;
          width: 0.16rem;
          height: 0.16rem;
          background: #ffe8ec;
          color: #ff0347;
        }
      }
      .linkTextActive {
        background-color: #ff375f;
        color: #fff;
      }
    }
  }
}
.video-info {
  padding: 0 0.12rem;
  h1 {
    margin-top: 0.24rem;
    font-size: 0.28rem;
    line-height: 0.32rem;
    color: rgba(0, 0, 0, 0.9);
    font-weight: 500;
    margin-bottom: 0.12rem;
    position: relative;
    span {
      font-size: 0.28rem;
      line-height: 0.32rem;
      color: rgba(0, 0, 0, 0.9);
      font-weight: 500;
      &::before {
        content: " - ";
      }
    }
  }
  .video-star {
    margin-bottom: 0.12rem;
    font-size: 0.14rem;
    .anticon {
      width: 0.24rem;
      height: 0.24rem;
    }
    span {
      line-height: 0.18rem;
      color: rgba(0, 0, 0, 0.8);
    }
  }
  .video-desc {
    color: rgba(0, 0, 0, 0.6);
    display: block;
    font-size: 0.14rem;
    font-weight: 400;
    line-height: 0.2rem;
    margin-bottom: 0.2rem;
    position: relative;
    word-break: break-word;
  }
  .tag-box {
    font-size: 0.12rem;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.6);
    grid-gap: 0.08rem;
    display: flex;
    margin-bottom: 0.1rem;
    .tag-item {
      border-radius: 0.12rem;
      padding: 0.04rem 0.1rem;
      color: rgba(0, 0, 0, 0.6);
      background-color: rgba(50, 50, 57, 0.07);
      border-radius: 1rem;
    }
  }
  .share-box {
    margin: 0 0 0.48rem;
    height: 0.4rem;
    .share-label {
      font-size: 0.18rem;
      font-weight: 500;
      color: rgba(0, 0, 0, 0.9);
      margin-right: 0.2rem;
    }
    img {
      width: 0.4rem;
      height: 0.4rem;
      border-radius: 50%;
      margin-right: 0.1rem;
      cursor: pointer;
      transition: all 0.3s;
      &:hover {
        transform: scale(1.1);
      }
    }
    .copyIcon {
      filter: grayscale(1);
      &:hover {
        filter: grayscale(0);
      }
    }
  }
}
.list-info {
  margin-bottom: 0.52rem;
  a {
    display: block;
  }
  .list-item {
    position: relative;
    height: 1.25rem;
    width: 100%;
    display: flex;
    border-radius: 0.08rem;
    padding: 0.04rem;
    margin-bottom: 0.08rem;
    background: #f2f2f3;
    border: 1px solid #f2f2f3;
    .img-box {
      flex-shrink: 0;
      width: 0.88rem;
      height: 1.17rem;
      border-radius: 0.03rem;
      position: relative;
      overflow: hidden;
      display: block;
      &:hover {
        img {
          transform: scale(1.1);
        }
      }
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: all 0.5s;
      }
    }
    .intro {
      display: flex;
      flex-direction: column;
      width: 120rem;
      justify-content: space-between;
      padding: 0.16rem 0.08rem;
      .title {
        font-size: 0.14rem;
        line-height: 0.2rem;
        color: rgba(0, 0, 0, 0.8);
        font-weight: 500;
        height: 0.4rem;
        width: 1.2rem;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        word-wrap: break-word;
      }
      .page-num {
        color: rgba(0, 0, 0, 0.4);
        line-height: 0.16rem;
        font-size: 0.14rem;
      }
    }
  }
  .see-more {
    backdrop-filter: blur(8px);
    color: rgba(0, 0, 0, 0.4);
    font-weight: 500;
    height: 1.25rem;
    width: 100%;
    color: rgba(0, 0, 0, 0.4);
    font-weight: 500;
    font-size: 0.16rem;
    padding: 0.04rem;
    margin-bottom: 0.08rem;
    background: #f2f2f3;
    border: 1px solid #f2f2f3;
    cursor: pointer;
    span {
      margin-right: 0.04rem;
    }
  }
}
@media (max-width: 768px) {
  .breadcrumb-container {
    li {
      display: flex;
      align-items: center;
    }
    a {
      flex-shrink: 0;
      font-size: 0.24rem;
      max-width: 1.5rem;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      transition: color 0.3s;
    }
    :deep(.ant-breadcrumb-link) {
      font-size: 0.24rem;
    }
  }
  .video-info {
    h1 {
      font-size: 0.28rem !important;
      font-weight: 500;
      color: rgba(0, 0, 0, 0.9);
      line-height: 0.56rem !important;
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      word-wrap: break-word;
      margin-bottom: 0.1rem;
      text-align: left;
      span {
        font-size: 0.24rem !important;
      }
    }
    .video-star {
      font-size: 0.24rem !important;
    }
    .share-box,
    .video-desc {
      display: none;
    }
    .tag-box {
      justify-content: center;
      .tag-item {
        font-size: 0.22rem;
        line-height: 0.3rem;
        padding: 0.05rem 0.12rem;
      }
    }
  }
  .h5-intro-box {
    padding: 0.48rem 0.32rem 0.24rem;
    .intro-title {
      font-size: 0.32rem;
      font-weight: 500;
      line-height: 0.38rem;
      margin-bottom: 0.16rem;
      color: rgba(0, 0, 0, 0.9);
      text-align: left;
    }
    .intro {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
      word-wrap: break-word;
      position: relative;
      font-size: 0.28rem;
      font-weight: 400;
      color: rgba(0, 0, 0, 0.6);
      line-height: 0.4rem;
      word-break: break-word;
      text-align: left;
    }
  }
  .episode-nav {
    position: relative;
    margin-bottom: 0.24rem;
    border-top: 0.08rem solid rgba(50, 50, 57, 0.07);
    border-bottom: 0.08rem solid rgba(50, 50, 57, 0.07);
    .catalog-box {
      padding: 0 0.32rem;
      background-color: #fff;
      height: 1rem;
      display: flex;
      align-items: center;
      justify-content: space-between;
      p {
        color: rgba(0, 0, 0, 0.9);
        font-size: 0.32rem;
        line-height: 0.38rem;
        margin-right: 0.08rem;
        span {
          font-size: 0.28rem;
          color: rgba(0, 0, 0, 0.6);
          line-height: 0.4rem;
        }
      }
      .anticon {
        color: #9e9e9e;
        font-size: 0.28rem;
      }
    }
    .share-box {
      height: 1rem;
      border-top: 0.08rem solid rgba(50, 50, 57, 0.07);
      background-color: #fff;
      padding-left: 0.32rem;
      display: flex;
      align-items: center;
      .share-label {
        margin-right: 0.2rem;
        color: rgba(0, 0, 0, 0.9);
        font-size: 0.32rem;
      }
      img {
        width: 0.6rem;
        height: 0.6rem;
        border-radius: 50%;
        margin-right: 0.2rem;
        cursor: pointer;
      }
    }
  }
}
</style>
